package gao.xiaolei.controller;

import java.io.IOException;

import gao.xiaolei.service.FileService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import gao.xiaolei.myExcepition.MyExcepition;

import javax.annotation.Resource;

@RestController
public class FileUploadController {

	@Resource(name = "FileService")
	private FileService fileService;

	@PostMapping("/")
	public String handleFileUpload(@RequestParam(name = "file", required = true) MultipartFile file,
									@RequestParam(name = "fileUuid", required = true) String fileUuid,
								   @RequestParam(name = "definePath", required = false) String definePath) throws IOException {
		return fileService.uploadFile(file,definePath,fileUuid);
	}

	@PostMapping("/string")
	public String handleFileUpload(@RequestParam(name = "fileString", required = true) String fileString,
								   @RequestParam(name = "fileUuid", required = true) String fileUuid,
								   @RequestParam(name = "definePath", required = false) String definePath) throws IOException {
		return fileService.uploadFileString(fileString,definePath,fileUuid);
	}


	@DeleteMapping("/{fileUuid}")
	public void handleFileDelete(@PathVariable(name="fileUuid",required = true) String fileUuid) {
		fileService.deleteFile(null,fileUuid);
	}

	@DeleteMapping("/{definePath}/{fileUuid}")
	public void handleFileDelete(@PathVariable(name="fileUuid",required = true) String fileUuid,
								 @PathVariable(name = "definePath",required = true) String definePath) {
		fileService.deleteFile(definePath,fileUuid);
	}

	@GetMapping("/{fileUuid}")
	public byte[] handleFileGet(@PathVariable(name="fileUuid",required = true) String fileUuid) throws IOException, MyExcepition {
		return fileService.getFile(null,fileUuid);
	}

	@GetMapping("/{definePath}/{fileUuid}")
	public byte[] handleFileGet(@PathVariable(name="fileUuid",required = true) String fileUuid,
								@PathVariable(name = "definePath",required = true) String definePath) throws IOException, MyExcepition {
		return fileService.getFile(definePath,fileUuid);
	}
	
	@PostMapping("/copy")
	public void handleFileCopy(@RequestParam(name="oldDefinePath",required = false) String oldDefinePath,
			@RequestParam(name="oldUuid",required = true) String oldUuid,
			@RequestParam(name="newDefinePath",required = false) String newDefinePath,
			@RequestParam(name="newUuid",required = true) String newUuid) throws IOException, MyExcepition {
		 fileService.copyFile(oldDefinePath, oldUuid, newDefinePath, newUuid);
	}
}
